package com.aispeech.jarpc.op.antispoofling;

import com.aispeech.jarpc.TextBin;
import com.aispeech.jarpc.op.AbstractRequest;
import lombok.NoArgsConstructor;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

@NoArgsConstructor
public class AntiSpooflingRequest extends AbstractRequest {
	private final static int BUFF_LEN = 3200;
	private final static String REQ_TMPL = "{\"op\":\"sed\"}";

	private InputStream audioStream;

	public AntiSpooflingRequest(InputStream audioStream) {
        this.audioStream = audioStream;
    }
	
	
    public TextBin toJsonBin() throws IOException {
        String json = toJson();
        
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        byte[] buff = new byte[BUFF_LEN];

        int read;
        while ((read = audioStream.read(buff)) > 0) {
            byteArrayOutputStream.write(buff, 0, read);
        }

        if (audioStream.markSupported())
            audioStream.reset();

        return new TextBin(json, byteArrayOutputStream.toByteArray());
        
    }

    @Override
    public String toJson() {
        return REQ_TMPL;
    }

    @Override
    public String OpType() {
        return "anti-spoofling";
    }
}
